FROM daocloud.io/library/centos:centos7
MAINTAINER qiang.zou@moji.com

ENV PHP_PREFIX /usr/local/php56
ENV NGX_PREFIX /usr/local/nginx
ENV ZK_PREFIX /usr/local/zookeeper/3.4.9

WORKDIR /root

# install package with doc
RUN sed -i '/tsflags=nodocs/d' /etc/yum.conf

RUN yum -y install epel-release

RUN yum update -y && \
    yum install -y iproute \
        python-setuptools \
        hostname \
        inotify-tools \
        yum-utils \
        which \
        jq \
        autoconf \
        automake \
        cmake \
        libtool \
        re2c \
        bison \
        bzip2-devel \
        bzip2 \
        libxml2-devel \
        libpng-devel \
        libjpeg-turbo-devel \
        curl-devel \
        pcre-devel \
        freetype-devel \
        libc-client-devel \
        libmcrypt-devel \
        libmcrypt \
        libmemcached-devel \
        libevent-devel \
        libevent \
        openssl-devel \
        openssl \
        tar \
        wget \
        vim && \

    yum clean all && \
    mkdir $PHP_PREFIX $NGX_PREFIX && \
    easy_install supervisor

RUN wget http://php.net/distributions/php-5.6.25.tar.bz2 \
        http://nginx.org/download/nginx-1.11.3.tar.gz \
        http://www-us.apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz \
        https://pecl.php.net/get/redis-2.2.8.tgz \
        https://pecl.php.net/get/memcached-2.2.0.tgz \
        https://pecl.php.net/get/yaf-2.3.5.tgz \
        https://pecl.php.net/get/swoole-1.8.8.tgz \
        https://pecl.php.net/get/mongodb-1.1.8.tgz \
        https://pecl.php.net/get/zookeeper-0.2.2.tgz

RUN tar -jxvf php-5.6.25.tar.bz2 && \
    tar -zxvf nginx-1.11.3.tar.gz && \
    tar -zxvf redis-2.2.8.tgz && \
    tar -zxvf memcached-2.2.0.tgz && \
    tar -zxvf yaf-2.3.5.tgz && \
    tar -zxvf swoole-1.8.8.tgz && \
    tar -zxvf mongodb-1.1.8.tgz && \
    tar -zxvf zookeeper-3.4.9.tar.gz && \

    # add user:group
    useradd -M -U www

# install php
WORKDIR /root/php-5.6.25
RUN ./configure --prefix=${PHP_PREFIX} \
    --enable-fpm \
    --enable-mbstring \
    --enable-sockets \
    --enable-opcache \
    --enable-pcntl \
    --enable-soap \
    --enable-zip \
    --enable-mysqlnd \
    --with-config-file-path=${PHP_PREFIX}/etc \
    --with-config-file-scan-dir=${PHP_PREFIX}/etc/conf.d \
    --with-fpm-user=www \
    --with-fpm-group=www \
    --with-mysql \
    --with-mysqli \
    --with-zlib \
    --with-bz2 \
    --with-gd \
    --with-pear \
    --with-mcrypt \
    --with-curl \
    --with-openssl \
    --with-jpeg-dir=/usr \
    --with-png-dir=/usr \
    --with-pdo-mysql && \
    make && make install && \
    cp php.ini-development ${PHP_PREFIX}/etc/php.ini && \
    mkdir ${PHP_PREFIX}/etc/conf.d && \
    cp ${PHP_PREFIX}/etc/php-fpm.conf.default ${PHP_PREFIX}/etc/php-fpm.conf

# install nginx
WORKDIR /root/nginx-1.11.3
RUN ./configure --prefix=${NGX_PREFIX} \
    --with-http_ssl_module \
    --user=www \
    --group=www && \
    make && make install

# compile zookeeper for php
WORKDIR /root/zookeeper-3.4.9/src/c
RUN ./configure --prefix=${ZK_PREFIX} && \
    make && make install

# {{ install php extensions
WORKDIR /root/redis-2.2.8
RUN ${PHP_PREFIX}/bin/phpize && \
    ./configure --with-php-config=${PHP_PREFIX}/bin/php-config && \
    make && make install && \
    echo 'extension=redis.so' > ${PHP_PREFIX}/etc/conf.d/redis.ini

WORKDIR /root/memcached-2.2.0
RUN ${PHP_PREFIX}/bin/phpize && \
    ./configure --with-php-config=${PHP_PREFIX}/bin/php-config && \
    make && make install && \
    echo 'extension=memcached.so' > ${PHP_PREFIX}/etc/conf.d/memcached.ini

WORKDIR /root/yaf-2.3.5
RUN ${PHP_PREFIX}/bin/phpize && \
    ./configure --with-php-config=${PHP_PREFIX}/bin/php-config && \
    make && make install && \
    echo 'extension=yaf.so' > ${PHP_PREFIX}/etc/conf.d/yaf.ini

WORKDIR /root/swoole-1.8.8
RUN ${PHP_PREFIX}/bin/phpize && \
    ./configure --with-php-config=${PHP_PREFIX}/bin/php-config && \
    make && make install && \
    echo 'extension=swoole.so' > ${PHP_PREFIX}/etc/conf.d/swoole.ini

WORKDIR /root/mongodb-1.1.8
RUN ${PHP_PREFIX}/bin/phpize && \
    ./configure --with-php-config=${PHP_PREFIX}/bin/php-config && \
    make && make install && \
    echo 'extension=mongodb.so' > ${PHP_PREFIX}/etc/conf.d/mongodb.ini

WORKDIR /root/zookeeper-0.2.2
RUN ${PHP_PREFIX}/bin/phpize && \
    ./configure --with-php-config=${PHP_PREFIX}/bin/php-config && \
    --with-libzookeeper-dir=${ZK_PREFIX}
    make && make install && \
    echo 'extension=zookeeper.so' > ${PHP_PREFIX}/etc/conf.d/zookeeper.ini
# }}

# Add supervisord conf, bootstrap.sh files
ADD container-files /
RUN cp /data/conf/nginx/nginx.conf ${NGX_PREFIX}/conf/nginx.conf && \
    cp /data/conf/php/php.ini ${PHP_PREFIX}/etc/php.ini

WORKDIR /root

VOLUME ["/data"]

ENTRYPOINT ["/config/bootstrap.sh"]
